<form class="layui-form detail_form form-table-edit" action="" lay-filter="template_copy_form">

    <input type="hidden" name="copy_template_id" value="#(detail.template_id)">

    <div class="layui-form-item">
        <label class="layui-form-label">新模板名称</label>
        <div class="layui-input-block">
            #set(name=detail.template_name.startsWith("【副本】")?detail.template_name:'【副本】'+detail.template_name)
            <input type="text" name="template_name" value="#(name)" lay-verify="required" placeholder="请输入模板副本名称" class="layui-input" autocomplete="off">
        </div>
    </div>

    <div class="detail_form_footer">
        <a class="layui-btn" lay-submit lay-filter="btn-template_copy-submit">创建副本</a>
        <a class="layui-btn layui-btn-primary" lay-filter="btn-template_copy-cancle">取消</a>
    </div>
</form>

<script>
    layui.use(['form', ], function () {
        var $ = layui.$
            , form = layui.form;

        form.render(null, 'template_copy_form');

        /* 自定义验证规则 */
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
        });

        /* 监听提交 */
        form.on('submit(btn-template_copy-submit)', function (data) {
            layer.msg('正在复制中，请稍候', {icon: 16});
            let formVals = data.field;
            $.ajax({
                method: 'post',
                url: "/generator/template/copy",
                data: formVals,
                dataType: 'json',
                success: function (rs) {
                    if (rs.success) {
                        layer.msg(rs.message || '复制成功', {icon: 6});
                        layui.renderTemplateList(function(){
                            let $block = $(`.template_list > div[tpl_id="${rs.id}"]`);
                            if ($block[0]) {
                                $block[0].scrollIntoView();
                                $block.css("box-shadow", "0px 0px 52px #1b00fb");
                                setTimeout(function () {
                                    $block.css("box-shadow", "");
                                }, 3000);
                            }
                        });
                        layer.closeAll('page'); //关闭所有页面层
                    } else {
                        layer.msg('复制失败,' + rs.message || '复制失败', {icon: 5});
                    }
                },
                error: function (a, b, c) {
                    layer.msg('请求异常，复制失败', {icon: 5});
                }
            });
            return false;
        });
        /* 监听取消 */
        $("[lay-filter=btn-template_copy-cancle]").click(function () {
            layer.closeAll("page");
        });

    });
</script>